package com.hub.eshop.service.impl;

import com.hub.eshop.common.ServiceResultEnum;
import com.hub.eshop.dao.NewBeeMallGoodsMapper;
import com.hub.eshop.entity.NewBeeMallGoods;
import com.hub.eshop.service.NewshopGoodsService;
import com.hub.eshop.util.BeanUtil;
import com.hub.eshop.util.PageQueryUtil;
import com.hub.eshop.util.PageResult;
import com.hub.eshop.vo.NewshopSearchGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewshopGoodsServiceImpl implements NewshopGoodsService {

    @Resource
    @Qualifier("newBeeMallGoodsMapper")
    private NewBeeMallGoodsMapper newBeeMallGoodsMapper;


    @Override
    public PageResult getNewBeeMallGoodsPage(PageQueryUtil pageUtil) {
        List<NewBeeMallGoods> goodsList = newBeeMallGoodsMapper.findNewBeeMallGoodsList(pageUtil);
        int total = newBeeMallGoodsMapper.getTotalNewBeeMallGoods(pageUtil);
        PageResult pageResult = new PageResult(goodsList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveNewBeeMallGoods(NewBeeMallGoods goods) {
        if (newBeeMallGoodsMapper.insertSelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public void batchSaveNewBeeMallGoods(List<NewBeeMallGoods> newBeeMallGoodsList) {
        if (!CollectionUtils.isEmpty(newBeeMallGoodsList)) {
            newBeeMallGoodsMapper.batchInsert(newBeeMallGoodsList);
        }
    }

    @Override
    public String updateNewBeeMallGoods(NewBeeMallGoods goods) {
        NewBeeMallGoods temp = newBeeMallGoodsMapper.selectByPrimaryKey(goods.getGoodsId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (newBeeMallGoodsMapper.updateByPrimaryKeySelective(goods) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public NewBeeMallGoods getNewBeeMallGoodsById(Long id) {
        return newBeeMallGoodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return newBeeMallGoodsMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public PageResult searchNewBeeMallGoods(PageQueryUtil pageUtil) {
        List<NewBeeMallGoods> goodsList = newBeeMallGoodsMapper.findNewBeeMallGoodsListBySearch(pageUtil);
        int total = newBeeMallGoodsMapper.getTotalNewBeeMallGoodsBySearch(pageUtil);
        List<NewshopSearchGoodsVO> newshopSearchGoodsVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)) {
            newshopSearchGoodsVOS = BeanUtil.copyList(goodsList, NewshopSearchGoodsVO.class);
            for (NewshopSearchGoodsVO newshopSearchGoodsVO : newshopSearchGoodsVOS) {
                String goodsName = newshopSearchGoodsVO.getGoodsName();
                String goodsIntro = newshopSearchGoodsVO.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28) {
                    goodsName = goodsName.substring(0, 28) + "...";
                    newshopSearchGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30) {
                    goodsIntro = goodsIntro.substring(0, 30) + "...";
                    newshopSearchGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(newshopSearchGoodsVOS, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }
}
